package com.sz.biz.logistics.ord.service.impl;

import com.google.common.collect.Lists;
import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.constants.TopServiceTypes;
import com.sz.biz.logistics.ord.entity.OrdServiceAssign;
import com.sz.biz.logistics.ord.service.OrdOrderServiceAssignService;
import com.sz.common.core.system.service.SysDictService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.biz.logistics.ord.entity.OrdOrderAdditional;
import com.sz.biz.logistics.ord.service.OrdOrderAdditionalService;
import com.sz.biz.logistics.ord.dto.OrdOrderAdditionalDto;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: 产品订单的附加服务 <br>
 * Author: hpeng.wang. <br>
 * Date: 2017-03-31 13:52:00.
 */
@Service("ordOrderAdditionalService")
public class OrdOrderAdditionalServiceImpl extends AbstractService implements OrdOrderAdditionalService {
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private OrdOrderServiceAssignService orderServiceAssignService;

    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.logistics.ord.entity.OrdOrderAdditionalMapper";
    }

    @Override
    public int save(OrdOrderAdditional entry) {
        dao.save(getSqlName("insertSelective"), entry);
        return entry.getId();
    }

    @Override
    public void batchSave(List<OrdOrderAdditional> ordOrderAdditionalList) {
        if (!CollectionUtils.isEmpty(ordOrderAdditionalList)) {
            dao.batchInsert(getSqlName("insert"), ordOrderAdditionalList);
            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("ADD_ORDER_ADDITION", ordOrderAdditionalList);
            PrincipalLogUtils.addOperationLog(ModuleNames.ORD, ModuleNames.ORD_ORDER_CARGO, UserActions.ADD, "添加订单可选服务", dataBackup);
        }
    }

    /**
     * 根据订单编号，删除下面可选的服务
     *
     * @param orderNo 订单编号
     */
    @Override
    public void deleteByOrderNo(String orderNo) {
        if (StringUtils.hasLength(orderNo)) {
            dao.delete(getSqlName("deleteByOrderNo"), orderNo);
        }

    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public void batchdelete(List<Integer> ids) {

    }

    @Override
    public void batchUpdate(List<OrdOrderAdditional> list) {

    }

    @Override
    public int update(OrdOrderAdditional entry) {
        return 0;
    }

    @Override
    public OrdOrderAdditional findById(Integer id) {
        return null;
    }

    @Override
    public List<OrdOrderAdditional> findByIds(List<Integer> ids) {
        return null;
    }

    @Override
    public List<OrdOrderAdditional> findByOrderNo(String orderNo) {
        List<OrdOrderAdditional> orderAdditionalList = new ArrayList<>();
        if (StringUtils.hasLength(orderNo)) {
            orderAdditionalList = dao.findForList(getSqlName("findByOrderNo"), orderNo, OrdOrderAdditional.class);
        }
        return orderAdditionalList;
    }

    @Override
    public List<String> findServiceUidByOrderNo(String orderNo) {
        List<String> serviceUidList = new ArrayList<>();
        if (StringUtils.hasLength(orderNo)) {
            serviceUidList = dao.findForList(getSqlName("findServiceUidByOrderNo"), orderNo, String.class);
        }
        return serviceUidList;
    }

    @Override
    public List<OrdOrderAdditional> findByOrderNos(List<String> orderNoList) {
        if (orderNoList == null || orderNoList.size() <= 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, orderNoList);
        }
        ParamData pd = new ParamData();
        pd.put("orderNos", orderNoList);
        List<OrdOrderAdditional> list = dao.findForList(getSqlName("findByOrderNos"), pd, OrdOrderAdditional.class);
        return list;
    }

    @Override
    public List<OrdOrderAdditionalDto> findDtoByOrderNo(String orderNo) {
        List<OrdOrderAdditional> ordOrderAdditionalList = dao.findForList(getSqlName("findByOrderNo"), orderNo, OrdOrderAdditional.class);
        Map<String, String> serviceTypeCodeMap = sysDictService.findMapByCatalog("biz.service.type");

        //组装OrdOrderAdditionalDto，并根据service的uid设置serviceName
        List<OrdOrderAdditionalDto> ordOrderAdditionalDtoList = new ArrayList<>();
        for (OrdOrderAdditional orderAdditional : ordOrderAdditionalList) {
            OrdOrderAdditionalDto orderAdditionalDto = new OrdOrderAdditionalDto();
            EntityUtils.copyPropertiesIgnoreNull(orderAdditional, orderAdditionalDto);
            //设置服务类型的名称
            if (!StringUtils.isEmpty(orderAdditional.getServiceTypeCode())) {
                String serviceTypeCodeName = serviceTypeCodeMap.get(orderAdditional.getServiceTypeCode());
                if (!StringUtils.isEmpty(serviceTypeCodeName)) {
                    orderAdditionalDto.setServiceTypeName(serviceTypeCodeName);
                }
            }

            ordOrderAdditionalDtoList.add(orderAdditionalDto);
        }
        return ordOrderAdditionalDtoList;
    }

    /**
     * 根据订单号集合，批量获取附加服务
     *
     * @param orderNos
     * @return
     */
    @Override
    public Map<String, List<OrdOrderAdditionalDto>> findMapDtosByOrderNos(List<String> orderNos) {
        List<OrdOrderAdditional> additionalList = findByOrderNos(orderNos);
        Map<String, List<OrdOrderAdditionalDto>> resultMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(additionalList)) {
            Map<String, String> serviceTypeCodeMap = sysDictService.findMapByCatalog("biz.service.type");

            for (OrdOrderAdditional orderAdditional : additionalList) {
                OrdOrderAdditionalDto orderAdditionalDto = new OrdOrderAdditionalDto();
                EntityUtils.copyPropertiesIgnoreNull(orderAdditional, orderAdditionalDto);
                //设置服务类型的名称
                if (!StringUtils.isEmpty(orderAdditional.getServiceTypeCode())) {
                    String serviceTypeCodeName = serviceTypeCodeMap.get(orderAdditional.getServiceTypeCode());
                    if (!StringUtils.isEmpty(serviceTypeCodeName)) {
                        orderAdditionalDto.setServiceTypeName(serviceTypeCodeName);
                    }
                }
                if (resultMap.containsKey(orderAdditional.getOrderNo())) {
                    resultMap.get(orderAdditional.getOrderNo()).add(orderAdditionalDto);
                } else {
                    List<OrdOrderAdditionalDto> dtos = Lists.newArrayList();
                    dtos.add(orderAdditionalDto);
                    resultMap.put(orderAdditional.getOrderNo(), dtos);
                }
            }
        }
        return resultMap;
    }


    @Override
    public Map<String, List<String>> findMapByOrderNos(List<String> orderNoList) {
        Map<String, List<String>> serviceUidMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(orderNoList)) {
            List<OrdOrderAdditional> orderAdditionalList = this.findByOrderNos(orderNoList);

            Map<String, OrdServiceAssign> serviceAssignMap = orderServiceAssignService.findMapByOrderNos(orderNoList);

            for (OrdOrderAdditional additional : orderAdditionalList) {
                if (serviceUidMap.get(additional.getOrderNo()) == null) {
                    List<String> serviceUidList = new ArrayList<>();
                    serviceUidMap.put(additional.getOrderNo(), serviceUidList);
                }

                String serviceUid = this.getServiceUidByAssign(serviceAssignMap, additional);
                serviceUidMap.get(additional.getOrderNo()).add(serviceUid);
            }
        }
        return serviceUidMap;
    }

    @Override
    public QResultDto findByParam(QueryParams params) {
        return null;
    }

    @Override
    public void checkCode(int sid, String code) {

    }

    @Override
    public void updateOrderNoByOrderNo(String oldOrderNo, String newOrderNo) {
        if (StringUtils.isEmpty(oldOrderNo) || StringUtils.isEmpty(newOrderNo)) {
            throw Exceptions.bizException(com.sz.biz.logistics.constants.ErrorCodes.ERROR_COMMON, "提交订单异常");
        }
        ParamData pd = new ParamData();
        pd.put("newOrderNo", newOrderNo);
        pd.put("oldOrderNo", oldOrderNo);
        dao.update(getSqlName("updateOrderNoByOrderNo"), pd);
    }


    private String getServiceUidByAssign(Map<String, OrdServiceAssign> serviceAssignMap, OrdOrderAdditional additional) {
        String serviceUid = "";
        if (CollectionUtils.isEmpty(serviceAssignMap)) {
            throw Exceptions.bizException(ErrorCodes.ERROR_COMMON, "多服务匹配未成功");
        }

        String serviceTypeCode = additional.getServiceTypeCode();
        OrdServiceAssign serviceAssign = serviceAssignMap.get(additional.getOrderNo());
        if (!ObjectUtils.isEmpty(serviceAssign)) {
            if (serviceTypeCode.contains(TopServiceTypes.RECEIVE)) {
                serviceUid = serviceAssign.getSt001Uid();
            } else if (serviceTypeCode.contains(TopServiceTypes.STORE)) {
                serviceUid = serviceAssign.getSt002Uid();
            } else if (serviceTypeCode.contains(TopServiceTypes.PORT)) {
                serviceUid = serviceAssign.getSt003Uid();
            } else if (serviceTypeCode.contains(TopServiceTypes.MAINLINE)) {
                serviceUid = serviceAssign.getSt004Uid();
            } else if (serviceTypeCode.contains(TopServiceTypes.DELIVERY)) {
                serviceUid = serviceAssign.getSt005Uid();
            } else if (serviceTypeCode.contains(TopServiceTypes.OTHER)) {
                serviceUid = serviceAssign.getSt006Uid();
            }
            if (StringUtils.isEmpty(serviceUid)) {
                throw Exceptions.bizException(ErrorCodes.ERROR_COMMON, "多服务匹配未成功！！！");
            }
        }


        return serviceUid;
    }

}
